const express = require('express')
const router = express.Router()

//1.路由级别中间件
router.use((req, res, next)=>{
	console.log('来自路由中间件')
	next()
})

function loginValidate (req, res, next) {
	let {name, password} = req.query
	console.log(req.query)
	if(!name || !password){
		res.send({
			message: '缺少用户名或者密码'
		})
	}else{
		next()
	}
}
//2.在路由内部使用中间件，针对单个路由处理
router.get('/userdemo', [loginValidate], (req, res, next)=>{
	console.log('/userdemo验证通过')
	res.send({
		msg: '用户名密码正确'
	})
	next()
})

module.exports = router